using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Fieldbook.Entities;
using System.Collections.Generic;
using Fieldbook.Business;

namespace Fieldbook.Testwebsite.User
{
    public partial class AddFieldWork : FieldbookPage
    {
        private List<Appuser> users = null;
        protected List<Operation> operations = null;
        protected List<Machine> machines = null;
        protected List<Resource> resources = null;
        protected List<Measureunit> fieldworkMU = null;
        protected List<Measureunit> allMeasureUnits = null;
        protected List<Field> houseHoldFields = null;

        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            users = AppUserController.LoadHouseHoldUsers((long)Session["userID"]);
            operations = OperationController.LoadOperations((long)Session["userID"]);
            machines = MachineController.LoadMachines((long)Session["userID"]);
            resources = ResourceController.LoadResources((long)Session["userID"]);
            fieldworkMU = MeasureUnitController.LoadMeasrueUnits(Measureunit.MeasureCategory.FieldWork);
            allMeasureUnits = MeasureUnitController.LoadAllMeasureUnits();
            houseHoldFields = FieldController.LoadHouseHoldFields((long)Session["userID"]);

            if (ddlFieldState.Items.Count == 0)
            {
                ddlFieldState.Items.Add(new ListItem());
                foreach (Field.FieldState value in Field.FieldState.GetValues(typeof(Field.FieldState)))
                {
                    ddlFieldState.Items.Add(
                        new ListItem(value.ToString(),
                            ((byte)value).ToString()));
                }
            }

            this.ddlExecutorID.DataSource = users;
            this.ddlExecutorID.DataTextField = "Firstname";
            this.ddlExecutorID.DataValueField = "Userid";
            this.ddlExecutorID.DataBind();
        }

        /// <summary>
        /// Add new field work
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkAddFieldWork_Command(object sender, CommandEventArgs e)
        {
            long userID = (long)Session["userID"];
            List<FieldWorkOperation> operations = new List<FieldWorkOperation>();
            List<FieldWorkMachine> machines = new List<FieldWorkMachine>();
            List<FieldWorkResource> resources = new List<FieldWorkResource>();

            FieldWork fw = new FieldWork();
            fw.FieldID = Int64.Parse(Request.Form["selectField"]);
            fw.ExecutionDate = DateTime.Parse(this.txtDate.Text);
            fw.ExecutorID = Int64.Parse(this.ddlExecutorID.SelectedValue);
            fw.LoggerID = userID;
            fw.LoggedDate = DateTime.Now;
            fw.Remarks = this.txtRemarks.Text;
            fw.TotalCost = Decimal.Parse(this.txtTotalCost.Text);
            Field.FieldState? fieldState = String.IsNullOrEmpty(this.ddlFieldState.SelectedValue) ? null : (Field.FieldState?)Byte.Parse(this.ddlFieldState.SelectedValue);

            if (!String.IsNullOrEmpty(Request.Form["operationCount"]))
            {
                int operationCount = Int32.Parse(Request.Form["operationCount"]);
                for (int i = 1; i <= operationCount; i++)
                {
                    if (!String.IsNullOrEmpty(Request.Form["hiddenOperationID" + i]))
                    {
                        FieldWorkOperation operation = new FieldWorkOperation();
                        operation.OperationID = Int64.Parse(Request.Form["hiddenOperationID" + i]);
                        operation.Cost = Decimal.Parse(Request.Form["txtTotalCost" + i]);
                        operations.Add(operation);
                    }
                }
            }
            if (!String.IsNullOrEmpty(Request.Form["machineCount"]))
            {
                long machineCount = Int32.Parse(Request.Form["machineCount"]);
                for (int i = 1; i <= machineCount; i++)
                {
                    if (!String.IsNullOrEmpty(Request.Form["hiddenMachineID" + i]))
                    {
                        FieldWorkMachine machine = new FieldWorkMachine();
                        machine.MachineID = Int64.Parse(Request.Form["hiddenMachineID" + i]);
                        machine.MeasureUnitID = Int32.Parse(Request.Form["machineMU" + i]);
                        machine.Quota = Decimal.Parse(Request.Form["txtMachineQuota" + i]);
                        machines.Add(machine);
                    }
                }
            }
            if (!String.IsNullOrEmpty(Request.Form["resourceCount"]))
            {
                long resourceCount = Int32.Parse(Request.Form["resourceCount"]);
                for (int i = 1; i <= resourceCount; i++)
                {
                    if (!String.IsNullOrEmpty(Request.Form["hiddenResourceID" + i]))
                    {
                        FieldWorkResource resource = new FieldWorkResource();
                        resource.ResourceID = Int64.Parse(Request.Form["hiddenResourceID" + i]);
                        resource.Quota = Decimal.Parse(Request.Form["txtResourceQuota" + i]);
                        resources.Add(resource);
                    }
                }
            }

            bool sucess = FieldController.InsertFieldWork(fw, fieldState, operations, machines, resources);
            if (sucess)
            {
                Response.Redirect("~/User/FieldList.aspx");
            }
            else
            {
                this.lblOperationStatus.Text = "Error inserting field work";
            }
            
        }
    }
}
